<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="/css/index.css">
</head>

<body>
    原密码：<input type="text" name="" id="crrentPassword"><br>
    新密码：<input type="password" name="" id="newPassword"><br>
    <button onclick="amend()">确认</button>
</body>
<script>
    const crrentPassword = document.querySelector('#crrentPassword')
    const newPassword = document.querySelector('#newPassword')
    const searchParams=new Map([window.location.search.slice(1).split('=')])
    const account=searchParams.get('account')

    crrentPassword.addEventListener('blur', () => {
        fetch('api/user/login', {
            method: 'POST',
            body: JSON.stringify({ account, password: crrentPassword.value }),
            headers: {
                'content-type': "application/json"
            }
        }).then(res => res.json()).then(res => {
            if (res.code !== 200) {
                alert('原密码错误')
            }
        })
    })

    function amend() {
        fetch('api/user/amend', {
            method: 'POST',
            body: JSON.stringify({ account, newPassword: newPassword.value }),
            headers: {
                'content-type': "application/json"
            }
        }).then(res => res.json()).then(res => {
            if (res.code !== 200) {
                alert('修改失败')
            } else {
                alert('修改成功')
                 window.location="http://localhost:3200/home";
            }
        })
    }
</script>

</html>